package com.hy.controller;

import com.hy.service.LoginOauthService;
import com.hy.utils.HY_Result;
import me.zhyd.oauth.model.AuthCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 是奕靜吖
 */
//@CrossOrigin
@RestController
@RequestMapping("/hy-frontuser-provider/oauth")
public class LoginOauthController {

    /**
     * 第三方登录 - 服务
     */
    @Autowired
    private LoginOauthService lgos;

    /**
     * 授权请求地址
     * @param source
     * @param response
     * @return
     */
    @RequestMapping("/render/{source}")
    public HY_Result renderAuth(@PathVariable String source, HttpServletResponse response){
        return lgos.renderAuth(source);
    }

    /**
     * 授权回调地址
     * @param source
     * @param callback
     * @param response
     * @return
     */
    @RequestMapping("/callback/{source}")
    public void login(@PathVariable String source, AuthCallback callback, HttpServletResponse response) throws IOException {
        //跳转地址
        response.sendRedirect(lgos.login(source,callback).getRedirectUrl());
    }

    /**
     * 取消授权登录
     * @param source 平台
     * @param token  token
     * @return
     */
    @RequestMapping("/revoke/{source}/{token}")
    public Object revokeAuth(@PathVariable String source, @PathVariable("token") String token){
        return lgos.revokeAuth(source,token);
    }

    /**
     * 刷新Token
     * @param source
     * @param token
     * @return
     */
    @RequestMapping("/refresh/{source}/{token}")
    public Object refreshAuth(@PathVariable String source, @PathVariable("token") String token){
        return lgos.refreshAuth(source,token);
    }

}